import type { PluginOption } from 'vite'
import { fileURLToPath, URL } from 'url'
import vue from '@vitejs/plugin-vue'
import boxen from 'boxen'
import picocolors from 'picocolors'
import AutoImport from 'unplugin-auto-import/vite'
import TurboConsole from 'unplugin-turbo-console/vite'
import Components from 'unplugin-vue-components/vite'
import banner from 'vite-plugin-banner'
import { compression } from 'vite-plugin-compression2'
import Pages from 'vite-plugin-pages'
import VueDevTools from 'vite-plugin-vue-devtools'
import VueSetupExtened from 'vite-plugin-vue-setup-extend'
import { manualChunksPlugin } from 'vite-plugin-webpackchunkname'
import pkg from '../package.json'

export const plugins: PluginOption[] = [
  vue(),
  // https://github.com/vuejs/devtools-next
  VueDevTools(),
  AutoImport({
    imports: ['vue', 'vue-router', '@vueuse/core', 'pinia'],
    dts: 'types/auto-imports.d.ts',
    dirs: ['src/stores', 'src/hooks'],
    eslintrc: {
      // 已存在文件设置默认 false，需要更新时再打开，防止每次更新都重新生成
      enabled: false,
      // 生成文件地址和名称
      filepath: fileURLToPath(new URL('../types/.eslintrc-auto-import.json', import.meta.url)),
      globalsPropValue: true
    }
  }),
  Components({
    dirs: ['src/components'],
    dts: 'types/components.d.ts'
    // resolvers: [VantResolver()],
  }),
  manualChunksPlugin(),
  compression({
    include: /\.(css|json|js|svg)$/
  }),
  VueSetupExtened(),
  Pages({
    dirs: 'src/views',
    exclude: ['**/components/**/*.vue']
  }),
  TurboConsole(),
  // 打包的时候添加版权注释
  banner({
    content: `/**\n * name: ${pkg.name}\n * version: v${pkg.version}\n * description: ${pkg.description}\n * author: ${pkg.author}\n * homepage: ${pkg.homepage}\n */`
  }),
  // 开发时候添加提示
  {
    name: 'vite-plugin-terminal-info',
    apply: 'serve',
    async buildStart() {
      const { bold, green, cyan, underline } = picocolors

      console.log(
        boxen(`${bold(green(`由一叶知秋开发`))}\n\n当前使用：${cyan('Vue3基础开发模板')}\n\n${bold('关注掘金：')}${underline('https://juejin.cn/user/2432534469180568')}`, {
          padding: 1,
          margin: 1,
          borderStyle: 'double',
          textAlignment: 'center'
        })
      )
    }
  }
]
